// pages/chooseBgm/chooseBgm.js
const app = getApp()
Page({
  onReady: function (e) {
    // 使用 wx.createAudioContext 获取 audio 上下文 context
    this.audioCtx = wx.createAudioContext('myAudio')
  },
  /**
   * 页面的初始数据
   */
  data: {
    bgmList: [],
    serverUrl: "",
    videoParams: {}

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (params) {

    var me = this;
    console.log(params);
    me.setData({
      videoParams: params
    });

    var user = app.getGlobalUserInfo();
    var serverUrl = app.serverUrl;
    wx.showLoading({
      title: '请等待...',
    });
    // 调用后端
    wx.request({
      
      url: serverUrl + '/bgm/list',
      method: "POST",
      header: {
        'content-type': 'application/json', // 默认值
        'userId': user.id,
        'userToken': user.userToken
      },
      success: function (res) {
        console.log(res.data);
        wx.hideLoading();
        if (res.data.status == 200) {
            var bgmList = res.data.data;
            me.setData({
              bgmList: bgmList,
              serverUrl: serverUrl
            });
        }
      }
    });    
  },

  upload: function(e) {
    var me = this;

    var bgmId = e.detail.value.bgmId;
    var desc = e.detail.value.desc;
    
    console.log("bgmId:" + bgmId);
    console.log("desc:" + desc);

    var duration = me.data.videoParams.duration;
    var tmpHeight = me.data.videoParams.tmpHeight;
    var tmpWidth = me.data.videoParams.tmpHeight;
    var tmpVideoUrl = me.data.videoParams.tmpVideoUrl;
    var tmpCoverUrl = me.data.videoParams.tmpCoverUrl;
    console.log(tmpCoverUrl);

    // 上传短视频
    wx.showLoading({
      title: '上传中...',
    });
    
    var serverUrl = app.serverUrl;
    // fixme 修改原有的全局对象为本地缓存
    var userInfo = app.getGlobalUserInfo();

    wx.uploadFile({
      url: serverUrl + '/video/upload',
      formData: {
        // userId: app.userInfo.id,
        userId: userInfo.id,
        bgmId: bgmId,
        desc: desc,
        videoSeconds: duration,
        videoHeight: tmpHeight,
        videoWidth: tmpWidth
      },
      filePath: tmpVideoUrl,
      name: 'file',
      header: {
        'content-type': 'application/json',
        'userId': userInfo.id,
        'userToken': userInfo.userToken
      },
      success: function (res) {
        console.log(res.data + res.data.status);
        var data = JSON.parse(res.data);
        wx.hideLoading();
        if (data.status == 200) {
          wx.showToast({
            title: '上传成功！~~',
            icon: "success"
          });
          wx.navigateBack({
                delta: 1
          });
          // debugger
          // wx.uploadFile({
          //   url: serverUrl + '/video/uploadCover',
          //   formData: {
          //     // userId: app.userInfo.id,
          //     userId: userInfo.id,
          //     videoId: videoId
          //   },
          //   filePath: tmpCoverUrl,
          //   name: 'file',
          //   header: {
          //     'content-type': 'application/json'
          //   },
          //   success: function (res) {
          //     var data = JSON.parse(res.data);
          //     wx.hideLoading();
          //     if (res.data.status == 200) {
          //       wx.showToast({
          //         title: '上传成功！~~',
          //         icon: "success"
          //       });
          //       wx.navigateBack({
          //         delta: 1
          //       })
          //     }else{
          //       wx.showToast({
          //         title: '上传失败！~~',
          //         icon: "fail"
          //       });
          //     }
          //   }
          // });
        }  else if (data.status == 502) {
            wx.showToast({
              title: res.data.msg,
              duration: 2000,
              icon: "none"
            });
            wx.redirectTo({
              url: '../userLogin/login',
            })
          } else {
            wx.showToast({
              title: '上传失败!~~',
              icon: "success"
            });
          }
      }
    });
  }

})